{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "71d30b4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_sercret = 'sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c7c4aebe",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "762aa8dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1657285109,05244c87-5f68-4569-9dfd-db7862ef6ef5',\n",
       " 'time_used': 327,\n",
       " 'faces': [{'face_token': '13ee645ea136b15cb06ff2bb3915313b',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "12c746ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1657285128,6904d19d-d889-4f38-896e-dfba32e595c8',\n",
       " 'time_used': 761,\n",
       " 'faces': [{'face_token': '4dd5777901a343dcff570074a7b2a246',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25a1c43f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9f3e7ece",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0f808bbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#A-0准备工作\n",
    "import requests\n",
    "API_KEY ='NhBooJ_1o9fLID_ulAEJFigqvwmnVbO3'\n",
    "API_S ='sBy3LVppVXQOpJoPqWmFQoHUxeqTimA0'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "464db001",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-1创建人脸数据集合\n",
    "FaceSet_url =\"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload ={\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_secret\":API_S\n",
    "}\n",
    "r = requests.post(url=FaceSet_url,params =payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "304770f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "135805b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2328003584d38e35567acfa12e58b626'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#A-2获取人脸数据库信息\n",
    "r.json()['faceset_token']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "92bd4561",
   "metadata": {},
   "outputs": [],
   "source": [
    "faceset_token = '2328003584d38e35567acfa12e58b626'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3c582e3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':face_token\n",
    "}\n",
    "r = requests.post(url = getdetail_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c1d42d61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "58acb93f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '53261128fa582b871fc889cdc7f7f180',\n",
       " 'tags': '',\n",
       " 'time_used': 75,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1647919079,d6600cc0-92b6-43ed-83f4-6f65591b768f',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cea01e78",
   "metadata": {},
   "outputs": [],
   "source": [
    "更新人脸数据库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "151d22e2",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_KEY' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-319bb507674d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mupdate_FaceSet_url\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m payload = {\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[1;34m'api_key'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mAPI_KEY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;34m'api_secret'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mAPI_S\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[1;34m'faceset_token'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mfaceset_token\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'API_KEY' is not defined"
     ]
    }
   ],
   "source": [
    "# C-1\n",
    "update_FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id':'API020305',\n",
    "    'display_name':'2020级网新API周二上午班',\n",
    "    'user_data':'59人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url = update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2579b73",
   "metadata": {},
   "outputs": [],
   "source": [
    "###1.实现人脸检测获取face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11df1c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "def face_detect(API_key,API_sercret,image_path):\n",
    "    \"\"\"该函数调用face++face_detect接口\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file':(image_path,open(image_path,'rb'),'multipart/form-data')}\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret\n",
    "    }\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3ea4dc24",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'r' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-d17ca1acc36c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mr\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'r' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2452ee36",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
